package com.ceres.customer.dao;

import com.ceres.customer.pojo.Menu;
import com.ceres.customer.pojo.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Component
public interface CustRoleMapper extends Mapper<Role> {

    //根据用户id在关联表中查询相关角色
    @Select("select * from t_role where category = 1 and id in (select rid from t_cust_user_role where cuid = #{id})")
    LinkedHashSet<Role> getByUserId(Integer id);

    //根据角色ID获取菜单

    @Select("select * from t_menu where category=1 and type=1 and pid is null and id in (select rid from t_role_menu where rid = #{id})")
    LinkedHashSet<Menu> getMenuByRoleId(Integer id);

    //根据角色ID获取权限
    @Select("select * from t_menu where category=1 and type=2 and id in (select rid from t_role_menu where rid = #{id})")
    Set<Menu> getPermByRoleId(Integer id);

    //在角色权限关联表中添加数据
    @Insert("insert into t_role_menu values (#{roleId},#{menuId})")
    void setRoleMenu(@Param("roleId")int roleId, @Param("menuId")int menuId);

    //删除角色权限关联信息
    @Delete("delete from t_role_menu where rid = #{id}")
    void deleteRoleMenuByRid(Integer id);

    //向角色权限关联表中添加信息
    @Insert("insert into t_role_menu values (#{roleId},#{menuId})")
    void addRoleMenu(@Param("roleId")Integer roleId, @Param("menuId")Integer menuId);

    //模糊查询
    @Select("<script>select * from t_role <if test=\"keyword!=null and keyword!=''\">where role=#{keyword}</if></script>")
    List<Role> findByKeyWord(@Param("keyword")String keyword);

    //查询所有平台用户
    @Select("select * from t_role where category = 1")
    List<Role> findAll();

    //根据用户id获取对应角色
    @Select("select * from t_role where id in (select rid from t_cust_user_role where cuid=#{id})")
    List<Role> findRoleByUserId(int id);
}
